<template>
  <div class="submitATool mt-6">
    <div class="rounded-lg px-4 pt-6 pb-7 box-shadow">
      <h3 class="text-2xl text-gray-1000 font-semibold text-center">
        {{$t('submit_toolsYouNeedToSubmit')}}
      </h3>
      <el-form :model="form"
        label-position="top"
        status-icon
        :rules="rules"
        ref="form"
        label-width="100px"
        class="max-w-xl mx-auto mt-6">
        <el-form-item :label="$t('toolName')"
          required="required"
          prop="name">
          <el-input v-model="form.name"
            :readonly="loading"
            autocomplete="off"
            :placeholder="$t('toolNamePlaceholder')"></el-input>
        </el-form-item>
        <el-form-item :label="$t('websiteURL')"
          required="required"
          prop="website">
          <el-input v-model="form.website"
            :readonly="loading"
            autocomplete="off"
            :placeholder="$t('pleaseEnterWebsiteURL')"></el-input>
        </el-form-item>
        <div class="flex justify-between items-center bg-gray-2600 px-4 sm:px-14 py-6 mt-7 submit-ctrl gap-4 flex-wrap rounded-sm">
          <div class="flex-shrink-0">
            <div class="text-3xl text-gray-1000 font-semibold"> {{$t('submit_totalNum', {num: 49})}}</div>
            <p class="text-sm text-gray-1500 mt-1"> {{$t('submit_noQueue')}}</p>
          </div>
          <el-button type="primary"
            class="flex-shrink-0 w-full sm:w-auto"
            :loading="loading"
            @click="submitForm()">
            {{$t('submit_payNum', {num: 49})}}
          </el-button>
        </div>
      </el-form>
    </div>

    <div class="box-shadow mt-4 p-4 sm:p-8 example">
      <div class="space-y-5 md:space-y-0 md:flex items-start gap-y-5 gap-x-7">
        <div class="w-full tool-pic">
          <img src="@/static/images/image_1.webp"
            alt="toolify"
            class="block"
            loading="lazy">
        </div>
        <div class="mt-5 sm:mt-0 w-full md:flex-1 md:w-0">
          <h3 class="text-2xl font-semibold text-gray-1000">{{$t('submit_submitAToolExample')}}</h3>
          <div class="mt-7 space-y-4.5">
            <p class="text-base text-gray-1000">{{$t('submit_youWillGet')}}</p>
            <div class="flex items-start gap-2.5">
              <svg-icon className="w-4.5 h-4.5 flex-shrink-0 mt-1"
                iconName='check_4'></svg-icon>
              <div class="flex-1 w-0">
                <div class="text-base font-semibold text-gray-1000">
                  {{$t('submit_appearedInListing')}}
                </div>
              </div>
            </div>
            <div class="flex items-start gap-2.5">
              <svg-icon className="w-4.5 h-4.5 flex-shrink-0 mt-1"
                iconName='check_4'></svg-icon>
              <div class="flex-1 w-0">
                <div class="text-base font-semibold text-gray-1000">
                  {{$t('submit_borderHighlight')}}
                </div>
                <p class="text-base text-gray-1000">
                  {{$t('submit_usersCanQuicklySpot')}}
                </p>
              </div>
            </div>
            <div class="flex items-start gap-2.5">
              <svg-icon className="w-4.5 h-4.5 flex-shrink-0 mt-1"
                iconName='check_4'></svg-icon>
              <div class="flex-1 w-0">
                <div class="text-base font-semibold text-gray-1000">
                  {{$t('submit_getDofollowLinks')}}
                </div>
                <p class="text-base text-gray-1000">
                  {{$t('submit_yourWebsiteWillGet')}}
                </p>
              </div>
            </div>
            <div class="flex items-start gap-2.5">
              <svg-icon className="w-4.5 h-4.5 flex-shrink-0 mt-1"
                iconName='check_4'></svg-icon>
              <div class="flex-1 w-0">
                <div class="text-base font-semibold text-gray-1000">
                  {{$t('submit_listingTrafficForeverSubmitAi')}}
                </div>
                <p class="text-base text-gray-1000">
                  {{$t('submit_yourToolStays')}}
                </p>
              </div>
            </div>
            <div class="flex items-start gap-2.5">
              <svg-icon className="w-4.5 h-4.5 flex-shrink-0 mt-1"
                iconName='check_4'></svg-icon>
              <div class="flex-1 w-0">
                <div class="text-base font-semibold text-gray-1000">
                  {{$t('submit_getToolifyAiLaunch')}}
                </div>
                <p class="text-base text-gray-1000">
                  {{$t('submit_getToolifysAI')}}
                </p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  name: 'SubmitATool',
  props: {},
  data() {
    return {
      form: {
        name: null,
        website: null,
      },
      rules: {
        name: [
          {
            required: true,
            message: this.$i18n.t('pleaseEnterToolName'),
            trigger: ['change', 'blur'],
          },
        ],
        website: [
          {
            required: true,
            message: this.$i18n.t('pleaseEnterWebsiteURL'),
            trigger: ['change', 'blur'],
          },
        ],
      },
      loading: false,
    }
  },
  computed: {
    ...mapState({
      locale: (state) => state.locale,
      toolify_isLogin: (state) => state.toolify_isLogin,
    }),
  },
  mounted() {
    if (process.browser) {
      let submitForm = localStorage.getItem('submitForm')
      if (submitForm) {
        this.form = JSON.parse(submitForm)
      }
      localStorage.removeItem('submitForm')
    }
  },
  methods: {
    submitForm() {
      if (!this.toolify_isLogin) {
        localStorage.setItem('submitForm', JSON.stringify(this.form))
        this.$router.replace(`/login?from=${this.$route.name}`)
        return false
      }

      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.loading = true
          try {
            let res
            let req = {}
            let langPath = `${this.locale !== 'en' ? `/${this.locale}` : ''}`
            req.success_url = `${window.location.origin}${langPath}/payment?type=submit`
            req.cancel_url = `${window.location.origin}${langPath}/submit`

            req.website = this.form.website
            req.name = String(this.form.name)
            res = await this.$axiosApi.submitWebsite(req)

            this.loading = false
            if (
              res &&
              res.code === 200 &&
              res.data &&
              res.data.stripe &&
              res.data.stripe.url
            ) {
              window.location.href = res.data.stripe.url
            }
          } catch (error) {
            this.loading = false
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.submitATool {
  .box-shadow {
    box-shadow: 0px 0px 14px 0px rgba(0, 0, 0, 0.06);
  }
  .el-button-group {
    .el-button {
      font-size: 16px;
      color: #191919;
      background: none;
      &:hover {
        border-color: #7150e6;
        background: none;
        color: #7150e6;
      }
      &.actived {
        border-color: #7150e6;
        background: none;
        color: #7150e6;
        z-index: 1;
      }
    }
  }
  .el-form {
    .el-form-item {
      margin-bottom: 24px;
      .el-form-item__label {
        font-size: 16px;
        font-weight: bold;
        color: #191919;
        line-height: 21px;
        padding-bottom: 14px;
      }
    }
  }
  .submit-ctrl {
    .el-button {
      font-size: 16px;
      max-width: 100%;
      padding-left: 30px;
      padding-right: 30px;
    }
  }

  .example {
    img {
      width: 100%;
    }
  }
}
@media (min-width: 768px) {
  .submitATool {
    .tool-pic {
      width: 37.5234521% !important;
      max-width: 400px;
      img {
        width: 100%;
        aspect-ratio: 4/5;
        image-rendering: pixelated;
      }
    }
  }
}
</style>
